package com.parking.carsystem.parkingchargesystem.activity;

import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.parking.carsystem.parkingchargesystem.R;
import com.parking.carsystem.parkingchargesystem.common.Constant;
import com.parking.carsystem.parkingchargesystem.module.GroupListModule;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import ttyy.com.recyclerexts.base.EXTRecyclerAdapter;
import ttyy.com.recyclerexts.base.EXTViewHolder;

/**
 * 添加车牌分组界面
 */
public class AddParkingPlateActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.text)
    TextView text;
    @BindView(R.id.edittext_group_name)
    EditText edittextGroupName;
    @BindView(R.id.line_one)
    TextView lineOne;
    @BindView(R.id.already_existing_group)
    TextView alreadyExistingGroup;
    @BindView(R.id.group_parking_data)
    RecyclerView groupParkingData;
    @BindView(R.id.line_two)
    TextView lineTwo;
    @BindView(R.id.add_paking)
    TextView addPaking;
    @BindView(R.id.card_view)
    CardView cardView;
    private EXTRecyclerAdapter<GroupListModule.DataBean> recyclerAdapter;
    private ArrayList<String> list;
    private List<GroupListModule.DataBean> beanList;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_add_parking_plate;
    }

    @Override
    public void initView() {
        back.setVisibility(View.VISIBLE);
        back.setOnClickListener(this);
        addPaking.setOnClickListener(this);
    }

    @Override
    public void initData() {
        text.setText("新增车牌分组");
        groupParkingData.setLayoutManager(new LinearLayoutManager(this));
        recyclerAdapter = new EXTRecyclerAdapter<GroupListModule.DataBean>(R.layout.item_plate_group) {
            @Override
            public void onBindViewHolder(EXTViewHolder extViewHolder, int i, GroupListModule.DataBean dataBean) {
                extViewHolder.setText(R.id.parking_name, dataBean.groupName);
            }
        };
        recyclerAdapter.setDatas(beanList);
        groupParkingData.setAdapter(recyclerAdapter);
        getGroupList();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                exitFinish();
                break;
            case R.id.add_paking:
                String groupName = edittextGroupName.getText().toString().trim();
                if (!TextUtils.isEmpty(groupName)) {
                    addParkingPlate(groupName);
                } else {
                    showToast("请输入添加的分组名字");
                }
                break;
        }
    }

    /**
     * 添加车牌分组
     *
     * @param groupName
     */
    private void addParkingPlate(final String groupName) {
        showLoading();
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("groupName", groupName);
        JSONObject jsonObject = new JSONObject(hashMap);
        OkGo.<String>post(Constant.url + Constant.GROUPADD)
                .upJson(jsonObject.toString())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        stopLoading();
                        try {
                            JSONObject jsonObject1 = new JSONObject(response.body());
                            int code = jsonObject1.getInt("code");
                            String msg = jsonObject1.getString("msg");
                            if (code == 200) {
                                GroupListModule.DataBean dataBean = new GroupListModule.DataBean();
                                dataBean.groupName = groupName;
                                beanList.add(dataBean);
                                recyclerAdapter.setDatas(beanList);
                                recyclerAdapter.notifyDataSetChanged();
                                edittextGroupName.setText("");
                            } else {
                                showToast(msg);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        stopLoading();
                        showToast(response.message());
                    }
                });
    }

    /**
     * 获取分组列表
     */
    private void getGroupList() {
        OkGo.<String>get(Constant.url + Constant.GROUP_LIST_DATA)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        GroupListModule groupListModule = gson.fromJson(response.body(), GroupListModule.class);
                        if (groupListModule.code == 200) {
                            beanList = groupListModule.data;
                            recyclerAdapter.setDatas(beanList);
                            recyclerAdapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {

                    }
                });
    }


    private void exitFinish() {
        setResult(Constant.ADDPARKINGGROUPRESULTCODE);
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exitFinish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
